package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxContainerList;
import com.adk.backend.service.ContainerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 集装箱管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/container")
public class ContainerController {
    
    @Autowired
    private ContainerService containerService;
    
    /**
     * 分页查询集装箱列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<Map<String, Object>>> getContainerList(
            @RequestParam(required = false) String loadingDate,
            @RequestParam(required = false) String month,
            @RequestParam(required = false) Integer vndTxId,
            @RequestParam(required = false) Integer vndBgId,
            @RequestParam(required = false) String loadingNo,
            @RequestParam(required = false) String bookingNo,
            @RequestParam(required = false) Integer typeId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("loadingDate", loadingDate);
        params.put("month", month);
        params.put("vndTxId", vndTxId);
        params.put("vndBgId", vndBgId);
        params.put("loadingNo", loadingNo);
        params.put("bookingNo", bookingNo);
        params.put("typeId", typeId);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<Map<String, Object>> result = containerService.getContainerList(params);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询集装箱详情
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxContainerList> getContainerDetail(@RequestParam Long id) {
        LxContainerList container = containerService.getContainerById(id);
        if (container != null) {
            return Result.success(container);
        }
        return Result.error("集装箱记录不存在");
    }
    
    /**
     * 创建集装箱
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxContainerList> createContainer(@RequestBody LxContainerList container) {
        try {
            LxContainerList result = containerService.createContainer(container);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建集装箱失败", e);
            return Result.error("创建集装箱失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新集装箱
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateContainer(@RequestBody LxContainerList container) {
        try {
            boolean success = containerService.updateContainer(container);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新集装箱失败", e);
            return Result.error("更新集装箱失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除集装箱
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteContainer(@RequestParam Long id) {
        try {
            boolean success = containerService.deleteContainer(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除集装箱失败", e);
            return Result.error("删除集装箱失败：" + e.getMessage());
        }
    }
}

